package com.mobileagri.backoffice.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

/**
 * Util class, returns a Map in the format Ext JS expects
 * 
 * 
 * @author LOUVET Romain
 */
@Component
public class ExtJSReturn {

	/**
	 * Generates modelMap to return in the modelAndView
	 * 
	 * @param <E>
	 * 
	 * @param contacts
	 * @return
	 */
	public static <E> Map<String, Object> mapOK(List<E> contacts, int total) {

		Map<String, Object> modelMap = new HashMap<String, Object>(3);
		modelMap.put("total", total);
		modelMap.put("data", contacts);
		modelMap.put("success", true);

		return modelMap;
	}

	public static <E> Map<String, Object> mapOK(List<E> contacts, int total,
			String callback) {

		Map<String, Object> modelMap = new HashMap<String, Object>(3);
		modelMap.put("total", total);
		modelMap.put("data", contacts);
		modelMap.put("callback", callback);
		modelMap.put("success", true);

		return modelMap;
	}

	/**
	 * Generates modelMap to return in the modelAndView in case of exception
	 * 
	 * @param msg
	 *            message
	 * @return
	 */
	public static Map<String, Object> mapError(String msg) {

		Map<String, Object> modelMap = new HashMap<String, Object>(2);
		modelMap.put("message", msg);
		modelMap.put("success", false);

		return modelMap;
	}

	public static Map<String, ? extends Object> mapOK(boolean add, int total) {
		Map<String, Object> modelMap = new HashMap<String, Object>(3);
		modelMap.put("total", total);
		modelMap.put("data", add);
		modelMap.put("success", true);

		return modelMap;
	}
}
